from django.http import HttpResponse
from django.shortcuts import render_to_response
from sicp.ex.models import *
# Create your views here.

def hello(request):
    return HttpResponse("Hello from Django!")

def people(request):
    all = People.objects.all()
    return render_to_response('people.html', {'all':all} )

def chapter(request, chpt_id):
    top = Chapter.objects.filter(identifier=chpt_id)[0]
    children = Chapter.objects.filter(parent=chpt_id)
    cl = []
    for c in children:
        exs = Exercise.objects.filter(chapter=c)
        cl.append({'chapter':c, 'exs': exs})
    return render_to_response('chapter.html', {'top':top, 'children':cl} )
    
    
def chapters(request):
    tops = Chapter.objects.filter(parent='SICP')
    all = []
    for parent in tops:
        children = Chapter.objects.filter(parent=parent.identifier)
        childrenl = []
        for c in children:
            childc = Chapter.objects.filter(parent=c.identifier)
            childrenl.append({'p':c, 'c':childc})
        all.append({'parent':parent, 'children':childrenl})
    return render_to_response('chapters.html', {'all':all} )

def default(request):
    return HttpResponse("Structure and Interpretation of Computer Programs")
